Page({
  data: {
    username: '',
    password: '',
    confirmPassword: '',
    phone: '',
  },

  onUsernameInput(event) {
    this.setData({ username: event.detail });
  },

  onPasswordInput(event) {
    this.setData({ password: event.detail });
  },

  onConfirmPasswordInput(event) {
    this.setData({ confirmPassword: event.detail });
  },

  onPhoneInput(event) {
    this.setData({ phone: event.detail });
  },

  handleRegister() {
    const { username, password, confirmPassword, phone } = this.data;

    if (!username || !password || !confirmPassword || !phone) {
      wx.showToast({ title: '请填写完整信息', icon: 'none' });
      return;
    }

    if (password !== confirmPassword) {
      wx.showToast({ title: '两次输入的密码不一致', icon: 'none' });
      return;
    }

    // 这里可以添加注册逻辑，比如调用 API 进行注册
    wx.request({
      url: 'http://localhost:8080/login/register', // 你的后台登录接口地址
      method: 'POST',
      data: this.data,
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        console.log(res)
        if (res.data.code === 0) {
          // 登录成功，保存 token
          wx.setStorageSync('token', res.data.token);
          wx.setStorageSync('userInfo', res.data.userInfo);

          wx.showToast({ title: '注册成功', icon: 'success' });

          // 跳转到首页或个人中心
          console.log('aaa');
          wx.reLaunch({ url: '/pages/login/login' });
        } else {
          wx.showToast({ title: res.data.message, icon: 'none' });
        }
      },
      fail() {
        wx.showToast({ title: '请求失败，请检查网络', icon: 'none' });
      }
    });

    // 注册成功后跳转到登录页面
    setTimeout(() => {
      wx.redirectTo({ url: '/pages/login/login' });
    }, 1500);
  }
});
